package com.echo.neocommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.echo.neocommunity.mapper.CommentMapper;
import com.echo.neocommunity.pojo.Comment;
import com.echo.neocommunity.pojo.DiscussPost;
import com.echo.neocommunity.service.CommentService;
import com.echo.neocommunity.utils.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    @Autowired
    private SensitiveFilter filter;
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private DiscussPostServiceImpl service;

    @Transactional(isolation= Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void addComment(Comment comment, DiscussPost discussPostId){
        comment.setContent(filter.filterText(HtmlUtils.htmlEscape(comment.getContent())));
        commentMapper.insert(comment);
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("entity_id",comment.getEntityId());
        Long count = commentMapper.selectCount(queryWrapper);
        discussPostId.setCommentCount(Math.toIntExact(count));
        service.updateById(discussPostId);
    }

}
